package com.grablist.feihu.grablist.ui.fragment.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.grablist.feihu.grablist.base.BaseBean;
import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.bean.BannerInfo;
import com.grablist.feihu.grablist.bean.ClueInfo;
import com.grablist.feihu.grablist.bean.SettingInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.ui.actviity.login.SelcetInfoAct;
import com.grablist.feihu.grablist.ui.fragment.biz.ClueBiz;
import com.grablist.feihu.grablist.ui.fragment.contract.ClueContract;

/**
 * Created by chenjian on 17/11/30.
 */

public class CluePresent implements ClueContract.Presenter {
    ClueContract.View view;
    ClueBiz clueBiz;

    private int index = 1;
    List<ClueInfo> clueInfoList = new ArrayList<>();

    @Inject
    public CluePresent(ClueContract.View view) {
        this.view = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        clueBiz = (ClueBiz) biz;
    }

    @Override
    public void toSelect() {
        view.startAct(SelcetInfoAct.class, null);
    }

    @Override
    public void GetBanner() {
        clueBiz.getBannerList(1, Constants.PAGESIZE, new BaseBiz.Callback<BaseBean<List<BannerInfo>>>() {
            @Override
            public void onSuccess(BaseBean<List<BannerInfo>> o) {
                if (o.getList().size() > 0) {
                    view.upImageList(o.getList());
                }
            }

            @Override
            public void onFailure(BaseBean<List<BannerInfo>> o) {

            }
        });
    }

    @Override
    public void getList(SettingInfo settingInfo) {
        view.loading(true);
        clueBiz.getClueList(MyApplication.userInfo.getUserCode(), index, Constants.PAGESIZE, settingInfo.getTrainingType(), settingInfo.getBusinessCircle(),  new BaseBiz.Callback<BaseBean<List<ClueInfo>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<ClueInfo>> o) {
                        clueInfoList.addAll(o.getList());
                        view.upDateList(clueInfoList);
                        if (index > 1) {
                            view.loadMoreComplete();
                        } else {
                            view.refreshComplete();
                        }
                        view.loading(false);
                        view.hasLoadMore(index * Constants.PAGESIZE >= o.getTotal());
                        index++;
                    }

                    @Override
                    public void onFailure(BaseBean<List<ClueInfo>> o) {
                        if (index > 1) {
                            view.loadMoreComplete();
                        } else {
                            view.refreshComplete();
                        }
                        view.loading(false);
                    }
                });
    }

    @Override
    public void refresh() {
        index = 1;
        clueInfoList.clear();
        view.upDateList(clueInfoList);
    }
}
